﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SalesSys.Comm.BLL;
using SalesSys.Comm.DAL;
using SalesSys.Comm.Utility;
using SalesSys.DAL.Models;

namespace SalesSys.BLL
{
    public class PaymentInfoBLL : BaseBLL<PaymentInfo, Guid>
    {
        public KeyValuePair<int, IEnumerable<PaymentInfo>> Select(string searchKey, FinaceApprovalState? state, string accounttype, ISimplePagingInfo qinfo, Guid? clientManagerId = null)
        {
            var query = Rep.Entities;
            if (!string.IsNullOrWhiteSpace(searchKey))
            {
                query = query.Where(p => p.Customer.Name.Contains(searchKey) || p.Customer.ShortName.Contains(searchKey));
            }
            if (state != null)
            {
                query = query.Where(p => p.State == state);
            }
            if (clientManagerId != null)
            {
                query = query.Where(p => p.CustomerAgent.Id == clientManagerId);
            }
            if (!string.IsNullOrEmpty((accounttype)))
            {
                //if (accounttype=="1")
                //{
                query = query.Where(p => p.Account.IsCash == (accounttype == "1"));
                //}
                //if (accounttype == "0")
                //{
                //    query = query.Where(p => p.Account.Name != "现金户头");
                //}
            }

            var count = query.Count();
            var list = query.OrderByDescending(p => p.CreateTime).Paging(qinfo.PageNum, qinfo.PageSize).ToList();
            return new KeyValuePair<int, IEnumerable<PaymentInfo>>(count, list);
        }

        /// <summary>
        /// 根据账户编号和状态，查询是否在审核中
        /// </summary>
        /// <param name="accountId"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public bool CheckExecuting(Guid accountId, Guid customerId, Guid customerAgentId, PaymentStatus status)
        {
            return Rep.Entities.Where(p => p.Customer.Id == customerId && p.CustomerAgent.Id == customerAgentId && p.Account.Id == accountId && p.PaymentStatus == status && p.State == FinaceApprovalState.Executing).FirstOrDefault() != null;
        }

        /// <summary>
        /// 申请退款有两个人员进行审批（sa和财务）
        /// </summary>
        /// <param name="accountId"></param>
        /// <param name="customerId"></param>
        /// <param name="customerAgentId"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public bool CheckTKExecuting(Guid accountId, Guid customerId, Guid customerAgentId, PaymentStatus status)
        {
            //查询sa是否审批通过
            var result=Rep.Entities.Where(p => p.Customer.Id == customerId && p.CustomerAgent.Id == customerAgentId && p.Account.Id == accountId && p.PaymentStatus == status && p.State == FinaceApprovalState.Executing).FirstOrDefault();
            if (result!=null)
            {
                return true;
            }
            else
            {
                //查询财务是否审批通过
                result = Rep.Entities.Where(p => p.Customer.Id == customerId && p.CustomerAgent.Id == customerAgentId && p.Account.Id == accountId && p.PaymentStatus == status && p.State == FinaceApprovalState.ApprovePass).FirstOrDefault();
                if (result != null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }
}
